package org.dyndns.opendemogroup.optimizer.operations;

import java.util.Random;

import org.dyndns.opendemogroup.optimizer.IOperation;
import org.dyndns.opendemogroup.optimizer.IOptimizationProblem;
import org.dyndns.opendemogroup.optimizer.Member;

/**
 * The simplest form of crossover, as illustrated in slide 21 of intro_to_GA.pdf
 */
public class OnePointCrossover implements IOperation
{

	public void perform ( Random randomSource, IOptimizationProblem problem,
			Member parentOne, Member parentTwo, Member childOne, Member childTwo )
	{
		int lastIndex = parentOne.getMemberSize ( ) - 1;
		// { perform crossover
		// exclude values of 0 and lastIndex
		int crossOverPoint = 1 + randomSource.nextInt ( lastIndex - 1 );
		childOne.copyFrom ( parentOne, 0, crossOverPoint );
		childOne.copyFrom ( parentTwo, crossOverPoint + 1, lastIndex );

		childTwo.copyFrom ( parentTwo, 0, crossOverPoint );
		childTwo.copyFrom ( parentOne, crossOverPoint + 1, lastIndex );
		// }
	}

}
